package com.axonactive.yolo.service.implement;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.axonactive.yolo.dao.interfaces.UserEmailRepository;
import com.axonactive.yolo.domain.UserEmail;
import com.axonactive.yolo.exception.UserEmailNotFoundException;
import com.axonactive.yolo.service.interfaces.UserEmailService;

@Service
public class UserEmailServiceImpl implements UserEmailService {

	private static Logger LOGGER = Logger.getLogger(UserEmailServiceImpl.class);

	@Resource
	private UserEmailRepository userEmailRepository;

	@Transactional
//	@CacheEvict(value = "listUserEmaiCache", allEntries = true)
	public List<UserEmail> findAll() {
		return userEmailRepository.findAll();
	}

	@Transactional
	public UserEmail create(UserEmail userEmail) {
		return userEmailRepository.save(userEmail);
	}

	@Transactional
//	@Cacheable(value = "listUserEmaiCache", key = "#id")
	public UserEmail findById(int id) {
		return userEmailRepository.findOne(id);
	}

	@Transactional(rollbackFor = UserEmailNotFoundException.class)
	public void delete(int id) throws UserEmailNotFoundException {
		UserEmail deletedUserEmail = userEmailRepository.findOne(id);

		if (deletedUserEmail == null)
			throw new UserEmailNotFoundException("User Email NotFoundException");

		userEmailRepository.delete(deletedUserEmail);
	}

	@Transactional(rollbackFor = UserEmailNotFoundException.class)
	public UserEmail update(UserEmail useremail)
			throws UserEmailNotFoundException {
		UserEmail updatedUserEmail = userEmailRepository.findOne(useremail
				.getId());
		if (updatedUserEmail == null)
			throw new UserEmailNotFoundException(
					"update User Email NotFoundException");
		updatedUserEmail.setIdUser(useremail.getIdUser());
		updatedUserEmail.setEmail(useremail.getEmail());
		updatedUserEmail.setActive(useremail.isActive());
		updatedUserEmail.setPrimary(useremail.isPrimary());
		updatedUserEmail.setActivationLinkParam(useremail
				.getActivationLinkParam());
		userEmailRepository.save(updatedUserEmail);
		return updatedUserEmail;
	}

	@Transactional
//	@Cacheable(value = "listUserEmaiCache", key = "#userId")
	public List<UserEmail> findAllEmailByUserId(int userId) {
		List<UserEmail> emails = userEmailRepository.findByIdUser(userId);
		return emails;
	}

	@Transactional
	public void deleteUserEmailByUserIdAndEmail(int userId, String email)
			throws UserEmailNotFoundException {
		List<UserEmail> userEmails = findAllEmailByUserId(userId);

		for (int i = 0; i < userEmails.size(); i++) {
			if (userEmails.get(i).getEmail().equals(email)) {
				delete(userEmails.get(i).getId());
			}
		}

	}

	@Override
	public boolean swithPrimaryToSecondaryEmail(int userId, String email)
			throws UserEmailNotFoundException {
		List<UserEmail> emails = findAllEmailByUserId(userId);

		for (int i = 0; i < emails.size(); i++) {
			if (emails.get(i).getEmail().equals(email)) {
				UserEmail userEmail = emails.get(i);

				if (userEmail.isPrimary()) {
					userEmail.setPrimary(false);
					update(userEmail);

					return true;
				} else if (!userEmail.isPrimary() && userEmail.isActive()) {
					userEmail.setPrimary(true);
					update(userEmail);

					return true;
				}

			}
		}

		return false;

	}

	@Override
//	@Cacheable(value = "listUserEmaiCache", key = "#str")
	public UserEmail findByEmailString(String str) {
		return userEmailRepository.findByEmail(str);
	}

	@Override
//	@Cacheable("listUserEmaiCache")
	public List<UserEmail> getUnactivedEmail() {
		return userEmailRepository.findByActive();
	}

	@Override
	public boolean setActivationAccountLinkParam(UserEmail userEmail,
			String param) {
		userEmail.setActivationLinkParam(param);
		try {
			update(userEmail);
			return true;
		} catch (UserEmailNotFoundException e) {
			LOGGER.info("User Email Not Found", e);
			return false;
		}
	}

}
